package me.zhengjie.modules.equipment.rest;

import me.zhengjie.aop.log.Log;
import me.zhengjie.modules.equipment.domain.TSparepart;
import me.zhengjie.modules.equipment.service.TSparepartService;
import me.zhengjie.modules.equipment.service.TWarehouseService;
import me.zhengjie.modules.equipment.service.dto.TSparepartQueryCriteria;
import me.zhengjie.modules.equipment.service.dto.TWarehouseDTO;
import me.zhengjie.modules.system.common.EnumSparepartStatus;
import me.zhengjie.utils.HttpResponseUtils;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.util.Optionals;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.sql.Timestamp;


/**
* @author sw
* @date 2019-07-06
*/
@RestController
@RequestMapping("api")
public class TSparepartController {

    @Autowired
    private TSparepartService tSparepartService;

    @Autowired
    private TWarehouseService tWarehouseService;

    @Log("查询TSparepart")
    @GetMapping(value = "/tSparepart")
    @PreAuthorize("hasAnyRole('ADMIN','TSPAREPART_ALL','TSPAREPART_SELECT')")
    public ResponseEntity getTSpareparts(TSparepartQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity(tSparepartService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @Log("新增TSparepart")
    @PostMapping(value = "/tSparepart")
    @PreAuthorize("hasAnyRole('ADMIN','TSPAREPART_ALL','TSPAREPART_CREATE')")
    public ResponseEntity create(@Validated @RequestBody TSparepart resources){
        resources.setStock_by(SecurityUtils.getName());   //入库人
        resources.setStock_time(new Timestamp(System.currentTimeMillis())); //入库时间
        resources.setState(EnumSparepartStatus.INSERT.getValue());

        try {
            TWarehouseDTO warehouseDTO = tWarehouseService.findById(resources.getWarehouseid());
            if (warehouseDTO != null) {
                resources.setWarehouse(warehouseDTO.getWarehousename());  //仓库名称
            }
        }catch (Exception ex) {

        }
        return new ResponseEntity(tSparepartService.create(resources),HttpStatus.CREATED);
    }

    @Log("修改TSparepart")
    @PutMapping(value = "/tSparepart")
    @PreAuthorize("hasAnyRole('ADMIN','TSPAREPART_ALL','TSPAREPART_EDIT')")
    public ResponseEntity update(@Validated @RequestBody TSparepart resources){
        try {
            TWarehouseDTO warehouseDTO = tWarehouseService.findById(resources.getWarehouseid());
            if (warehouseDTO != null) {
                resources.setWarehouse(warehouseDTO.getWarehousename());  //仓库名称
            }
        }catch (Exception ex) {

        }
        tSparepartService.update(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    @Log("删除TSparepart")
    @DeleteMapping(value = "/tSparepart/{id}")
    @PreAuthorize("hasAnyRole('ADMIN','TSPAREPART_ALL','TSPAREPART_DELETE')")
    public ResponseEntity delete(@PathVariable Integer id){
        try {
            tSparepartService.delete(id);
            return new ResponseEntity(HttpStatus.OK);
        }catch (Exception ex) {
            ex.printStackTrace();
            return HttpResponseUtils.passDefaultResponse(ex);
        }
    }
}